﻿package UIComponents.Messages
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * 消息显示屏 
	 * @author 邓坚
	 * 
	 */	
	public class ListMessageItems extends Sprite
	{
		public var _useMessageAry:Array;                  //准备显示数组
		public var _useMessageAllAry:Array;               //所有显示项数组
		public var _showType:uint=0;                      //显示型 0全部，1礼物，2求赠，3互动，付费   消息类型（3为消费，4为互动，5为求赠，1为礼物）
		
		public function ListMessageItems()
		{
			super();
            init();
		}
		
		public function init():void
		{
			_useMessageAry    = new Array();
			_useMessageAllAry = new Array();

			this.setNull();
			
			(getChildByName("scrollBar") as ScrollBar).maxNum = this._useMessageAry.length; 		//设置数量，改变滚动块长度
			(getChildByName("scrollBar") as ScrollBar).percentage = 0;               				//把滚动条设为0
			reShow();
			
			addListener();
		}
		
		
		
		
		/**
		 * 加一条消息 
		 * @param useMessage
		 * 
		 */
		public function addItem(umo:Object):void
		{
			var um:UseMessage	= new UseMessage(umo.Content,umo.ztFriendName,umo.ztMessageTime,umo.ztMessageType,umo.ztStatus,umo.ztFriendPic,umo.ztId);
			_useMessageAllAry.push(um);
		}
		
		/**
		 * 显示，跟据百分比 （显示的是准备显示项数组）
		 * @param pct
		 * 
		 */
		public function reShow(pct:uint=0):void
		{
			setNull();
			var start:uint = 0;
			if(_useMessageAry.length>4)
			{
				start = Math.floor(pct/100 * (_useMessageAry.length-4) );
			}
			var itemI:uint = 1;
			
			for(var i:uint = start; itemI<=4 && i<_useMessageAry.length; i++,itemI++)
			{
				var messageItem:MessageItem = getMessageItem(itemI);
				messageItem.setItem(_useMessageAry[i]);
				(getChildByName("new"+itemI) as Sprite).visible = (UseMessage)(_useMessageAry[i]).ztStatus;         //设置是否显示“新消息”
				messageItem._btnC.visible						 = (UseMessage)(_useMessageAry[i]).ztStatus;
			}
			
			
		}
		
		/**
		 * 返回第1-4个显示框 
		 * @param index
		 * @return 
		 * 
		 */
		public function getMessageItem(index:uint):MessageItem
		{
			if(index<1 || index>4)throw new Error("索引错误");
		    return this.getChildByName("messageItem"+index) as MessageItem;
		}
		
		
		/**
		 * 所有显示框消空 
		 * 
		 */
		public function setNull():void
		{
			var messageItem:MessageItem;
			for(var i:uint=1; i<=4; i++)
			{
				messageItem = getMessageItem(i);
				messageItem.setNull();
				(getChildByName("new"+i) as Sprite).visible = false;
			}
		}
		
		public function cleanMessage():void
		{
			this._useMessageAllAry.length	=	0;
			this.setNull();
		}

		
		/**
		 * 跟据消息类型，设置准备显示数组 
		 * 
		 */
		public function messageFilter():void
		{
			_useMessageAry.length = 0;
			for(var i:uint=0; i<_useMessageAllAry.length; i++){
				if(_showType!=0 && _useMessageAllAry[i].ztMessageType!=_showType )
					continue;
				_useMessageAry.push(_useMessageAllAry[i]);
			}
			
			_useMessageAry.sortOn("messageTime",Array.DESCENDING|Array.NUMERIC);
		}
		
		public function addListener():void
		{
			
			//过滤器事件
			getChildByName("messageType0").addEventListener(MouseEvent.CLICK,clickMessageTypeHandler);    
			getChildByName("messageType1").addEventListener(MouseEvent.CLICK,clickMessageTypeHandler);
			getChildByName("messageType2").addEventListener(MouseEvent.CLICK,clickMessageTypeHandler);
			getChildByName("messageType3").addEventListener(MouseEvent.CLICK,clickMessageTypeHandler);
			getChildByName("messageType4").addEventListener(MouseEvent.CLICK,clickMessageTypeHandler);
			
			addEventListener("deleteId",deleteHandler);
			addEventListener("clickbtn",clickBtnHandler);
		}

		public function clickBtnHandler(e:PanelEvent):void
		{
			this.dispatchEvent(new PanelEvent("deleteId",e._id,true,false,e._type) );
			
		}
		
		public function deleteHandler(e:PanelEvent):void
		{
			clickHandler(e);
			return;
			
			for(var i:uint=0; i<_useMessageAllAry.length; i++)
				if(_useMessageAllAry[i].ztId == e._id)
				{
					_useMessageAllAry.splice(i,1);
					messageFilter();                                                           			//设置准备显示数组
					(getChildByName("scrollBar") as ScrollBar).maxNum = _useMessageAry.length; 		//设置数量，改变滚动块长度
					//(getChildByName("scrollBar") as ScrollBar).percentage += 2/(getChildByName("scrollBar") as ScrollBar).maxNum;
					reShow( (getChildByName("scrollBar") as ScrollBar).percentage );   
					
					break;
				}
			
			this.dispatchEvent(new PanelEvent("clickMessage",e._id,true,false,e._type) );
		}
		
		public function clickHandler(e:PanelEvent):void
		{
//			for(var i:uint=0; i<_useMessageAllAry.length; i++)
//				if(_useMessageAllAry[i].ztId == e._id)
//				{
//					(_useMessageAllAry[i] as UseMessage).ztStatus	=	false;
//					(getChildByName("scrollBar") as ScrollBar).maxNum = _useMessageAry.length; 		//设置数量，改变滚动块长度
//					reShow( (getChildByName("scrollBar") as ScrollBar).percentage );  
//					break;
//				}
//			this.setShowBtn(e._id,false);
			this.dispatchEvent(new PanelEvent("clickMessage",e._id,true,false,e._type) );
		}
		/**
		 * 设置过滤类型 
		 * @param e
		 * 
		 */  //显示型 0全部，1礼物，2求赠，3互动，4付费   消息类型（3为消费，4为互动，5为求赠，1为礼物）
		public function clickMessageTypeHandler(e:Event):void
		{   
              switch(e.currentTarget.name)
			  {
				  case "messageType0":
					  _showType=0;
					  break;
				  case "messageType1":
					  _showType=1;
					   break;
				  case "messageType2":
					  _showType=5;
					  break;
				  case "messageType3":
					  _showType=4;
					  break;
				  case "messageType4":
					  _showType=3;
					  break;
			  }
			  
			  messageFilter();                                                           				//设置准备显示数组
			  (getChildByName("scrollBar") as ScrollBar).maxNum = this._useMessageAry.length; 		//设置数量，改变滚动块长度
			  (getChildByName("scrollBar") as ScrollBar).percentage = 0;               				//把滚动条设为0
	          reShow();           		                                               					//显示
			  
			  e.stopPropagation();
			
		}
		
		public function showAll():void
		{
			_showType=0;
			messageFilter();                                                           				//设置准备显示数组
			(getChildByName("scrollBar") as ScrollBar).maxNum = this._useMessageAry.length; 		//设置数量，改变滚动块长度
			(getChildByName("scrollBar") as ScrollBar).percentage = 0;               				//把滚动条设为0
			reShow();    
		}
		
		/**
		 * 设置id条消息是否显示按扭
		 */
		public function setShowBtn(id:uint,isShow:Boolean=true):void
		{
			for(var i:uint=0; i<_useMessageAllAry.length; i++)
				if(_useMessageAllAry[i].ztId == id)
				{
					(_useMessageAllAry[i] as UseMessage).ztStatus	=	isShow;
					(getChildByName("scrollBar") as ScrollBar).maxNum = _useMessageAry.length; 		//设置数量，改变滚动块长度
					reShow( (getChildByName("scrollBar") as ScrollBar).percentage );  
					break;
				}
		}
		
		/**
		 *删除一条消息 
		 * @param id
		 * 
		 */
		public function deleteMessageItem(id:uint):void
		{
			for(var i:uint=0; i<_useMessageAllAry.length; i++)
				if(_useMessageAllAry[i].ztId == id)
				{
					_useMessageAllAry.splice(i,1);
					
					(getChildByName("scrollBar") as ScrollBar).maxNum = _useMessageAry.length; 		//设置数量，改变滚动块长度
					reShow( (getChildByName("scrollBar") as ScrollBar).percentage );  
					break;
				}
		}
		
	}
}